package com.ruoyi.datastatistics.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.datastatistics.domain.PublicShareComsumeReport;
import com.ruoyi.datastatistics.mapper.PublicShareComsumeReportMapper;
import com.ruoyi.datastatistics.service.IPublicShareComsumeReportService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 公摊报Service业务层处理
 *
 * @author gdqt
 * @date 2025-05-13
 */
@Service
public class PublicShareComsumeReportServiceImpl extends ServiceImpl<PublicShareComsumeReportMapper, PublicShareComsumeReport> implements IPublicShareComsumeReportService
{
    @Autowired
    private PublicShareComsumeReportMapper publicShareComsumeReportMapper;

    /**
     * 查询公摊报
     *
     * @param id 公摊报主键
     * @return 公摊报
     */
    @Override
    public PublicShareComsumeReport selectPublicShareComsumeReportById(Long id)
    {

        return publicShareComsumeReportMapper.selectById(id);
    }

    /**
     * 查询公摊报列表
     *
     * @param publicShareComsumeReport 公摊报
     * @return 公摊报
     */
    @Override
    public List<PublicShareComsumeReport> selectPublicShareComsumeReportList(PublicShareComsumeReport publicShareComsumeReport)
    {
        LambdaQueryWrapper<PublicShareComsumeReport> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(publicShareComsumeReport.getTenantId())){
            queryWrapper.eq(PublicShareComsumeReport::getTenantId,publicShareComsumeReport.getTenantId());
        }
        if(!StringUtils.isEmpty(publicShareComsumeReport.getUserId())){
            queryWrapper.eq(PublicShareComsumeReport::getUserId,publicShareComsumeReport.getUserId());
        }
        if(!StringUtils.isEmpty(publicShareComsumeReport.getPublicShareId())){
            queryWrapper.eq(PublicShareComsumeReport::getPublicShareId,publicShareComsumeReport.getPublicShareId());
        }
        if(publicShareComsumeReport.getEnergyType() != null){
            queryWrapper.eq(PublicShareComsumeReport::getEnergyType,publicShareComsumeReport.getEnergyType());
        }
        if(publicShareComsumeReport.getPublicShareAmount() != null){
            queryWrapper.eq(PublicShareComsumeReport::getPublicShareAmount,publicShareComsumeReport.getPublicShareAmount());
        }
        if(publicShareComsumeReport.getPublicShareCost() != null){
            queryWrapper.eq(PublicShareComsumeReport::getPublicShareCost,publicShareComsumeReport.getPublicShareCost());
        }
        if(!StringUtils.isEmpty(publicShareComsumeReport.getReportDate())){
            queryWrapper.eq(PublicShareComsumeReport::getReportDate,publicShareComsumeReport.getReportDate());
        }
        if(publicShareComsumeReport.getReportType() != null){
            queryWrapper.eq(PublicShareComsumeReport::getReportType,publicShareComsumeReport.getReportType());
        }
        if(!StringUtils.isEmpty(publicShareComsumeReport.getCreatedBy())){
            queryWrapper.eq(PublicShareComsumeReport::getCreatedBy,publicShareComsumeReport.getCreatedBy());
        }
        if(publicShareComsumeReport.getCreatedTime() != null){
            queryWrapper.eq(PublicShareComsumeReport::getCreatedTime,publicShareComsumeReport.getCreatedTime());
        }
        if(!StringUtils.isEmpty(publicShareComsumeReport.getUpdatedBy())){
            queryWrapper.eq(PublicShareComsumeReport::getUpdatedBy,publicShareComsumeReport.getUpdatedBy());
        }
        if(publicShareComsumeReport.getUpdatedTime() != null){
            queryWrapper.eq(PublicShareComsumeReport::getUpdatedTime,publicShareComsumeReport.getUpdatedTime());
        }
        if(publicShareComsumeReport.getLocationNb() != null){
            queryWrapper.eq(PublicShareComsumeReport::getLocationNb,publicShareComsumeReport.getLocationNb());
        }
        return publicShareComsumeReportMapper.selectList(queryWrapper);
    }

    /**
     * 新增公摊报
     *
     * @param publicShareComsumeReport 公摊报
     * @return 结果
     */
    @Override
    public int insertPublicShareComsumeReport(PublicShareComsumeReport publicShareComsumeReport)
    {
        return publicShareComsumeReportMapper.insert(publicShareComsumeReport);
    }

    /**
     * 新增公摊消费-电表
     * @param frequencyType
     * @param freezeTime
     * @param userId
     * @return
     */
    @Override
    public int insertPublicShareComsumeReportForElectricityTerminals(String frequencyType,String freezeTime, String userId)
    {
        if("1".equals(frequencyType)){
            return publicShareComsumeReportMapper.insertForElectricityTerminalsForOne(freezeTime,"0");
        }else if("2".equals(frequencyType)){
            return publicShareComsumeReportMapper.insertForElectricityTerminalsForTwo(freezeTime.substring(0,7),freezeTime,userId);
        }else {
            return 0;
        }

    }

    /**
     * 新增公摊消费-水表
     * @param frequencyType
     * @param freezeTime
     * @param userId
     * @return
     */
    @Override
    public int insertPublicShareComsumeReportForWaterTerminals(String frequencyType,String freezeTime, String userId)
    {
        if("1".equals(frequencyType)){
            return publicShareComsumeReportMapper.insertForWaterTerminalsForOne(freezeTime,"0");
        }else if("2".equals(frequencyType)){
            return publicShareComsumeReportMapper.insertForWaterTerminalsForTwo(freezeTime.substring(0,7),freezeTime,userId);
        }else {
            return 0;
        }
    }
    /**
     * 修改公摊报
     *
     * @param publicShareComsumeReport 公摊报
     * @return 结果
     */
    @Override
    public int updatePublicShareComsumeReport(PublicShareComsumeReport publicShareComsumeReport)
    {
        return publicShareComsumeReportMapper.updateById(publicShareComsumeReport);
    }

    /**
     * 批量删除公摊报
     *
     * @param ids 需要删除的公摊报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareComsumeReportByIds(Long[] ids)
    {
        return publicShareComsumeReportMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除公摊报信息
     *
     * @param id 公摊报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareComsumeReportById(Long id)
    {
        return publicShareComsumeReportMapper.deleteById(id);
    }

    @Override
    public List<PublicShareComsumeReport> selectTotalReportList(PublicShareComsumeReport publicShareComsumeReport) {
        return publicShareComsumeReportMapper.selectTotalReportList(publicShareComsumeReport);
    }

    @Override
    public List<PublicShareComsumeReport> selectReportDetailList(PublicShareComsumeReport publicShareComsumeReport) {
        return publicShareComsumeReportMapper.selectReportDetailList(publicShareComsumeReport);
    }

    @Override
    public List<PublicShareComsumeReport> selectComsumeDetailList(PublicShareComsumeReport publicShareComsumeReport) {
        // if(publicShareComsumeReport.getEnergyType() == 1){
        //     return publicShareComsumeReportMapper.selectComsumeDetailList1(publicShareComsumeReport);
        // }else if(publicShareComsumeReport.getEnergyType() == 2){
        //     return publicShareComsumeReportMapper.selectComsumeDetailList2(publicShareComsumeReport);
        // }
        // return new ArrayList<>();
        return  publicShareComsumeReportMapper.selectComsumeDetailList(publicShareComsumeReport);
    }

    @Override
    public Map selectComsumeDetailListSum(PublicShareComsumeReport publicShareComsumeReport) {
        return publicShareComsumeReportMapper.selectComsumeDetailListSum(publicShareComsumeReport);
    }

    @Override
    public List<PublicShareComsumeReport> publicShareListForBill(PublicShareComsumeReport publicShareComsumeReport) {
        return publicShareComsumeReportMapper.publicShareListForBill(publicShareComsumeReport);
    }
}
